<template>
  <div class="container">
    <div class="logo"><img alt="" src="~@/assets/logo.png"></div>
    <div class="brand">中英石油</div>
    <div class="wellcome">
      <div class="title">WELCOME</div>
      <div class="sub-title">欢迎使用中英石油收银系统</div>
    </div>
    <div class="login-form">
      <div class="form-box">
        <div class="box-title">系统登录</div>
        <el-form ref="dataForm" :model="dataForm" :rules="dataFormRules" :show-message="false" class="data-form"
                 status-icon>
          <el-form-item label="账户" prop="loginName">
            <el-input ref="userNameRef" v-model="dataForm.loginName" clearable placeholder="请输入用户账号"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="dataForm.password" clearable placeholder="请输入用户密码" type="password"></el-input>
          </el-form-item>
          <el-form-item label="副屏">
            <el-switch v-model="autoShowSecondaryScreen" active-color="#13ce66" active-text="显示" inactive-color="grey"
                       inactive-text="隐藏"></el-switch>
          </el-form-item>
          <el-form-item>
            <el-button class="login-button" round @click="login">登 陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {Loading} from 'element-ui'
import {login as userLogin} from '@/api/user'
import sha256 from 'js-sha256'
import getMAC from 'getmac'

export default {
  name: 'Login',
  data () {
    return {
      loadingInstance: null,
      dataForm: {
        loginName: '',
        password: ''
      },
      dataFormRules: {
        loginName: [{required: true, message: '请输入用户名称', trigger: 'change'}],
        password: [{required: true, message: '请输入用户密码', trigger: 'change'}]
      }
    }
  },
  computed: {
    autoShowSecondaryScreen: {
      get () {
        return this.$store.getters.autoShowSecondaryScreen
      },
      set (value) {
        this.$store.dispatch('toggleAutoShowSecondaryScreen')
      }
    }
  },
  mounted () {
    const self = this
    // 监听回车事件
    document.onkeydown = function (e) {
      if (!e) {
        e = window.event
      }
      if ((e.keyCode || e.which) === 13) {
        if (self.$route.path === '/login') {
          self.login()
        }
      }
    }
    this.$store.dispatch('setSerialNumber', getMAC())
    // 焦点聚焦在用户名称输入框内
    this.$nextTick(() => {
      this.$refs['userNameRef'].focus()
    })
  },
  methods: {
    login () {
      this.loadingInstance = Loading.service({fullscreen: true, text: '登录中'})
      var loginForm = JSON.parse(JSON.stringify(this.dataForm))
      loginForm.password = sha256(loginForm.password)
      userLogin(loginForm).then(res => {
        console.log(res)
        if (this.autoShowSecondaryScreen) {
          this.openSecondaryScreen()
        }
        res.data.permissions = res.data.authBtnList
        this.$store.dispatch('clearReceiptAndVoiceList')
        this.$store.dispatch('userLogin', res.data)
        this.$store.dispatch('setSaleFuelRecordVisible', false)
        this.$store.dispatch('setGunStatusList', [])
        setTimeout(() => {
          this.$router.push('/')
          this.loadingInstance.close()
        }, 1100)
      }).catch(err => {
        console.log(err)
        this.loadingInstance.close()
        this.$message.warning(err)
      })
    },
    openSecondaryScreen () {
      const BrowserWindow = require('electron').remote.BrowserWindow
      const winURL = process.env.NODE_ENV === 'development'
        ? `http://localhost:9080`
        : `file://${__dirname}/index.html`
      var newWindow = new BrowserWindow({
        height: document.body.clientHeight,
        useContentSize: true,
        width: document.body.clientWidth,
        webPreferences: {
          nodeIntegration: true,
          enableRemoteModule: true,
          webviewTag: true
        }
      })
      newWindow.setMenu(null)
      newWindow.loadURL(winURL + '?sub=1')
      newWindow.on('closed', () => {
        newWindow = null
      })
    }
  }
}
</script>

<style scoped>
.container {
  width: 100%;
  height: 100%;
  background-image: url("../assets/bg-login.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  align-items: center;
}

.logo {
  position: absolute;
  top: 25px;
  left: 25px;
}

.brand {
  position: absolute;
  top: 25px;
  line-height: 50px;
  left: 80px;
  font-size: 1.6rem;
  color: white;
  font-weight: bold;
}

.logo img {
  width: 50px;
  height: 50px;
  border-radius: 10%;
}

.wellcome {
  position: absolute;
  width: 40%;
  top: 0;
  left: 10%;
  bottom: 0;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin-top: -200px;
}

.wellcome .title {
  margin-top: 50px;
  font-size: 6rem;
  /* font-weight: bold; */
}

.wellcome .sub-title {
  margin-top: 30px;
  font-size: 2rem;
  font-weight: bold;
}

.login-form {
  position: absolute;
  width: 40%;
  top: 0;
  left: 50%;
  right: 10%;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form .form-box {
  width: 360px;
  height: 480px;
  background: #FFF;
  border-radius: 10px;
  text-align: center;
}

.box-title {
  margin-top: 30px;
  height: 50px;
  line-height: 50px;
  font-size: 1.5rem;
  font-weight: bold;
}

.data-form {
  margin-top: 40px;
  width: 80%;
  margin-left: 10%;
}

.login-button {
  width: 230px;
  color: white;
  background: linear-gradient(90deg, #5b27da, #8c4fff);
  font-size: 1.2rem;

}

.el-button.is-round {
  border-radius: 30px;
  padding: 18px 23px;
}

.el-button.is-round:hover {
  color: white;
  background: linear-gradient(90deg, #5b27da, #8c4fff);
}

.el-button.is-round:focus {
  color: white;
  background: linear-gradient(90deg, #5b27da, #8c4fff);
}

@media (max-width: 1000px) {
  .wellcome {
    width: 40%;
    left: 10;
  }

  .login-form {
    width: 40%;
    right: 10;
  }
}

@media (min-width: 1800px) {
  .wellcome {
    width: 30%;
    left: 20%;
  }

  .login-form {
    width: 30%;
    right: 20%;
  }
}
</style>
